package com.keon.jianbao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.keon.jianbao.AppManager;
import com.keon.jianbao.R;
import com.keon.jianbao.adapter.HomeActivityAdapter;
import com.keon.jianbao.adapter.HomeExpertAdapter;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseFragment;
import com.keon.jianbao.bean.BannberList;
import com.keon.jianbao.bean.Banner;
import com.keon.jianbao.bean.BeanActivity;
import com.keon.jianbao.bean.BeanActivityList;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.bean.ExpertGridList;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.enu.ListType;
import com.keon.jianbao.enu.WebType;
import com.keon.jianbao.ui.activity.ListActivity;
import com.keon.jianbao.ui.activity.WebActivity;
import com.keon.jianbao.util.TDevice;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import cn.lightsky.infiniteindicator.InfiniteIndicatorLayout;
import cn.lightsky.infiniteindicator.slideview.BaseSliderView;
import cn.lightsky.infiniteindicator.slideview.DefaultSliderView;

/**
 * Created by keon on 2015/9/1.
 */
public class HomeFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    int hideWaitDialog = 3;

    @Bind(R.id.home_ad_view_pager)
    InfiniteIndicatorLayout vpAd;
    @Bind(R.id.home_expert_list)
    GridView gvExpertList;
    @Bind(R.id.home_activity_list)
    ListView lvActivityList;
    @Bind(R.id.home_expert_more)
    TextView tvExpertMore;

    ArrayList<Expert> expertList = null;
    ArrayList<BeanActivity> activityList = null;

    AsyncHttpResponseHandler adsHandler = new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    BannberList bannberList = new BannberList(result.getContent());
                    initViewPager(bannberList);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            ((BaseActivity) AppManager.getAppManager().currentActivity()).showToast(R.string.common_error_network);
        }

        @Override
        public void onFinish() {
            super.onFinish();

            hideWaitDialog--;
            if (hideWaitDialog == 0) {
                ((BaseActivity) AppManager.getAppManager().currentActivity()).hideWaitDialog();
            }
        }
    };

    AsyncHttpResponseHandler expertHandler = new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
                    ExpertGridList expertGridList = new ExpertGridList(result.getContent());
                    initGridView(expertGridList);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            ((BaseActivity) AppManager.getAppManager().currentActivity()).showToast(R.string.common_error_network);
        }

        @Override
        public void onFinish() {
            super.onFinish();

            hideWaitDialog--;
            if (hideWaitDialog == 0) {
                ((BaseActivity) AppManager.getAppManager().currentActivity()).hideWaitDialog();
            }
        }
    };

    AsyncHttpResponseHandler activityHandler = new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            try {
                Result result = new Result(responseBody);
                if (result.getStatus() > 0) {
//                    Activi expertGridList = new ExpertGridList(result.getContent());
                    BeanActivityList activityList = new BeanActivityList(result.getContent());
                    initListView(activityList);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            ((BaseActivity) AppManager.getAppManager().currentActivity()).showToast(R.string.common_error_network);
        }

        @Override
        public void onFinish() {
            super.onFinish();

            hideWaitDialog--;
            if (hideWaitDialog == 0) {
                ((BaseActivity) AppManager.getAppManager().currentActivity()).hideWaitDialog();
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(View view) {
        tvExpertMore.setOnClickListener(this);
        lvActivityList.setOnItemClickListener(this);
        gvExpertList.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
//        showWaitDialog();
        Api.ads(adsHandler);
        Api.expert_lists("", 1, 4, 2, expertHandler);
        Api.activity_lists(1, 4, 2, activityHandler);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.home_expert_more:
                Bundle bundle = new Bundle();
                BaseActivity ba = (BaseActivity) getActivity();

                bundle.putInt("type", ListType.EXPERTGRIDLIST2.getId());
                ba.jump(ListActivity.class, bundle);
                break;
        }

    }

    @Override
    public void onPause() {
        super.onPause();
        vpAd.stopAutoScroll();
    }

    @Override
    public void onResume() {
        super.onResume();
        vpAd.startAutoScroll();
    }

    private void initViewPager(BannberList bannberList) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) vpAd.getLayoutParams();
        lp.height = (int) (300 * ((double) TDevice.getDisplayMetrics().widthPixels / 750));
        vpAd.setLayoutParams(lp);


        HashMap<String, String> url_maps = new HashMap<String, String>();
        List l = bannberList.getList();
        for (int i = 0; i < l.size(); i++) {
            Banner banner = (Banner) l.get(i);
            url_maps.put(banner.getName(), banner.getContent());
        }

        for (String name : url_maps.keySet()) {
            DefaultSliderView textSliderView = new DefaultSliderView(getActivity());
            textSliderView
                    .image(url_maps.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .showImageResForEmpty(R.mipmap.page_icon_empty)
                    .showImageResForError(R.mipmap.page_icon_network);
//                    .setOnSliderClickListener(this);
            textSliderView.getBundle()
                    .putString("extra", name);
            vpAd.addSlider(textSliderView);
        }
        vpAd.setScrollDurationFactor(0.7);
        vpAd.setIndicatorPosition(InfiniteIndicatorLayout.IndicatorPosition.Center_Bottom);
        vpAd.startAutoScroll();
    }

    private void initGridView(ExpertGridList expertGridList) {
        expertList = (ArrayList) expertGridList.getList();
        HomeExpertAdapter adapter = new HomeExpertAdapter(expertList);

        gvExpertList.setAdapter(adapter);
    }

    private void initListView(BeanActivityList list) {
        activityList = (ArrayList) list.getList();
        HomeActivityAdapter adapter = new HomeActivityAdapter(activityList);

        lvActivityList.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        int id = adapterView.getId();

        BaseActivity ba = (BaseActivity) getActivity();
        Bundle bundle = new Bundle();
        switch (id) {
            case R.id.home_expert_list:

                Expert expert = expertList.get(i);

                bundle.putInt("type", WebType.EXPERT_INTRO.getId());
                bundle.putInt("expert_id", expert.getId());
                ba.jump(WebActivity.class, bundle);
                break;
            case R.id.home_activity_list:

                BeanActivity activity = activityList.get(i);

                bundle.putInt("type", WebType.ACTIVITY_SHOW.getId());
                bundle.putInt("activity_id", activity.getId());
                ba.jump(WebActivity.class, bundle);
                break;
        }
    }
}
